package com.ztf0337.mycurriculumdesign.adapter;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.ztf0337.mycurriculumdesign.R;
import com.ztf0337.mycurriculumdesign.Utils.CompareClassUtil;
import com.ztf0337.mycurriculumdesign.Utils.TransitionUtils;
import com.ztf0337.mycurriculumdesign.entity.Order;
import com.ztf0337.mycurriculumdesign.entity.ParkingSpace;
import com.ztf0337.mycurriculumdesign.service.ParkingSpaceService;

import java.util.ArrayList;
import java.util.List;

public class ParkingSpaceAdapter extends RecyclerView.Adapter<ParkingSpaceAdapter.ViewHolder> {

    private List<ParkingSpace> parkingSpaceList;
    private OnItemClickListener listener;  // 监听器引用
    private List<ParkingSpace> selectPSs = new ArrayList<>();

    public ParkingSpaceAdapter(List<ParkingSpace> parkingSpaceList) {
        this.parkingSpaceList = parkingSpaceList;
    }

    // 定义点击事件接口
    public interface OnItemClickListener {
        void onItemClick(ParkingSpace parkingSpace);
    }


    // 提供设置监听器的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    private boolean isSelectionMode = false;

    // 提供外部调用的方法
    @SuppressLint("NotifyDataSetChanged")
    public void setSelectionMode(boolean selectionMode) {
        isSelectionMode = selectionMode;
        notifyDataSetChanged(); // 通知刷新所有 item
    }

    @NonNull
    @Override
    public ParkingSpaceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_parking_space, parent, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ParkingSpaceAdapter.ViewHolder holder, int position) {
        ParkingSpace parkingSpace = parkingSpaceList.get(position);
        holder.parkingInfoTextView.setText(parkingSpace.getParkingInfo());
        holder.tvStatusAndType.setText((parkingSpace.getStatus() ? "空闲" : "占用")
                + "   "
                + TransitionUtils.transParkType(parkingSpace.getType()));
        // 给每个item绑定点击事件，调用回调接口

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(parkingSpace);
            }
        });
        holder.checkBox.setVisibility(isSelectionMode ? View.VISIBLE : View.GONE);
        // 先解绑避免复用时错乱
        holder.checkBox.setOnCheckedChangeListener(null);
        // 这里设置勾选状态，判断选中列表中是否包含当前order的id
        holder.checkBox.setChecked(selectPSs.contains(parkingSpace));
        holder.checkBox.setOnClickListener(v -> {
            if (holder.checkBox.isChecked()) {
                if (!selectPSs.contains(parkingSpace)) {
                    selectPSs.add(parkingSpace);
                }
            } else {
                selectPSs.remove(position);
            }
        });

    }

    @Override
    public int getItemCount() {
        return parkingSpaceList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView parkingInfoTextView, tvStatusAndType;
        CheckBox checkBox;

        public ViewHolder(View view) {
            super(view);
            parkingInfoTextView = view.findViewById(R.id.tvParkingInfo);
            tvStatusAndType = view.findViewById(R.id.tvStatusAndType);
            checkBox = view.findViewById(R.id.cbSelect);
        }
    }

    // 更新整个数据集
    @SuppressLint("NotifyDataSetChanged")
    public void updateData(List<ParkingSpace> newData) {
        this.parkingSpaceList = newData;
        notifyDataSetChanged();  // 简单刷新全部（低效）
    }

    // 只新增一项
    public void addItem(ParkingSpace newItem) {
        parkingSpaceList.add(newItem);
        notifyItemInserted(parkingSpaceList.size() - 1);
    }

    // 修改一项
    public void updateItem(ParkingSpace newItem) {
        parkingSpaceList.set(CompareClassUtil.compareParkingSpace(parkingSpaceList,newItem), newItem);
        notifyItemChanged(CompareClassUtil.compareParkingSpace(parkingSpaceList,newItem));
    }

    // 删除一项
    public void removeItem(ParkingSpace newItem) {
        int index = CompareClassUtil.compareParkingSpace(parkingSpaceList, newItem);
        if (index != -1) {
            parkingSpaceList.remove(index);
            notifyItemRemoved(index);
        }
    }

        public List<ParkingSpace> getSelectPSsAndClear() {
        List<ParkingSpace> selected = new ArrayList<>(selectPSs); // 先复制一份当前选中ID
        selectPSs.clear();  // 清空原列表
        return selected;        // 返回复制的列表
    }

    public void clearSelect() {
        selectPSs.clear();  // 清空原列表
    }

}
